﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RTM.API;

namespace Hoguet.RTM
{
    public class RTMServiceFacade : IRtmServiceFacade
    {
        private readonly IRTMService _rtmService;
        private readonly IRtmTaskFacadeConverter _converter;
        private readonly bool _filterOutCompleted;

        public RTMServiceFacade(IRTMService rtmService, IRtmTaskFacadeConverter converter, bool filterOutCompleted)
        {
            _rtmService = rtmService;
            _converter = converter;
            _filterOutCompleted = filterOutCompleted;
        }

        public IEnumerable<RtmTaskFacade> GetTasks(string rtmSearch)
        {
            //if (_filterOutCompleted)
            //{
            //    rtmSearch += " NOT completed:";
            //}
            
            //when listId is null, it doesn't filter to a specific list
            //when lastSync is null, it doesn't filter on date changed
            var tasks = _rtmService.Tasks_GetList(listId: null, filter: rtmSearch, lastSync: null);
            
            //these are lists of task series or tasks... lets get it into task series and then get someone else to convert it the rest of the way
            return _converter.Convert(tasks.List.SelectMany(taskList => taskList.Series));
        }
    }
}
